
<template>
  <div>
    <el-row :gutter="20">
      <el-col :span="12">
        <el-card>
          <template #header>
            <span>门店扫码收货</span>
          </template>
          <el-form :model="receiveForm" label-width="100px">
            <el-form-item label="门店">
              <el-select v-model="receiveForm.storeId" placeholder="请选择门店">
                <el-option label="朝阳门店" value="ST001" />
                <el-option label="静安店" value="ST002" />
              </el-select>
            </el-form-item>
            <el-form-item label="商品条码">
              <el-input v-model="receiveForm.barcode" placeholder="请扫码或输入条码" />
            </el-form-item>
            <el-form-item label="温度">
              <el-input-number v-model="receiveForm.temperature" :min="-10" :max="10" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="handleReceive">确认收货</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </el-col>
      
      <el-col :span="12">
        <el-card>
          <template #header>
            <span>智能补货</span>
          </template>
          <el-form :model="replenishForm" label-width="100px">
            <el-form-item label="门店">
              <el-select v-model="replenishForm.storeId" placeholder="请选择门店">
                <el-option label="朝阳门店" value="ST001" />
                <el-option label="静安店" value="ST002" />
              </el-select>
            </el-form-item>
            <el-form-item label="商品">
              <el-input v-model="replenishForm.item" placeholder="商品名称" />
            </el-form-item>
            <el-form-item label="当前库存">
              <el-input-number v-model="replenishForm.currentStock" :min="0" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="handleReplenish">生成补货单</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </el-col>
    </el-row>
    
    <el-card style="margin-top: 20px;">
      <template #header>
        <span>门店列表</span>
      </template>
      <el-table :data="stores" style="width: 100%">
        <el-table-column prop="name" label="门店名称" />
        <el-table-column prop="location" label="地址" />
        <el-table-column prop="scanRate" label="扫码率">
          <template #default="{ row }">
            <el-progress :percentage="row.scanRate" :color="getScanRateColor(row.scanRate)" />
          </template>
        </el-table-column>
        <el-table-column prop="lastDelivery" label="最后配送时间" />
      </el-table>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import axios from 'axios'

const stores = ref([])
const receiveForm = ref({
  storeId: '',
  barcode: '',
  temperature: 2
})
const replenishForm = ref({
  storeId: '',
  item: '',
  currentStock: 0
})

const loadStores = async () => {
  const response = await axios.get('/api/store/list')
  stores.value = response.data
}

const handleReceive = async () => {
  await axios.post('/api/store/receive', receiveForm.value)
  alert('收货成功！')
  receiveForm.value = { storeId: '', barcode: '', temperature: 2 }
}

const handleReplenish = async () => {
  const response = await axios.post('/api/store/replenish', replenishForm.value)
  alert(`补货单已生成：${response.data.replenishment.id}`)
  replenishForm.value = { storeId: '', item: '', currentStock: 0 }
}

const getScanRateColor = (rate) => {
  return rate >= 98 ? '#67C23A' : '#E6A23C'
}

onMounted(() => {
  loadStores()
})
</script>
